package com.zt.ztrob.utils;

import java.io.UnsupportedEncodingException;
import java.util.Iterator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class GetGroupNickName {

	public String getNickName(JSONArray a,String name){
		String nickname=null;
		Iterator<Object> it = a.iterator();
	       
        while (it.hasNext()) {
            JSONObject ob = (JSONObject) it.next();
           
            if(ob.getString("UserName").equals(name)){
              // System.out.println(ob.getString("NickName"));
               nickname=ob.getString("NickName");
               return nickname;
            }
        }
		return null;
	}
	public String getGroupCountAndOwner(String groupNickName,JSONArray b,String fromusername){
	//	if(b!=null){
		try {
			
		
		Iterator<Object> it = b.iterator();
	       String qunOwner=null;//群管理员
        while (it.hasNext()) {
            JSONObject ob = (JSONObject) it.next();
           
            
            if(ob.getString("UserName").equals(fromusername)){
            	
	            if(ob.getString("NickName")!=null){
		                System.out.println("群名字:"+ob.getString("NickName"));
		            }
	            if(ob.getString("MemberCount")!=null){
	            	System.out.println("群人数:"+ob.getString("MemberCount"));
	            }
            if(ob.getString("ChatRoomOwner")!=null){
            	JSONArray memberList=JSON.parseArray(ob.getString("MemberList"));
				Iterator<Object> it2 = memberList.iterator();
			       
			        while (it2.hasNext()) {
			        		JSONObject ob2 = (JSONObject) it2.next();
				        	if(ob2.getString("UserName").equals(ob.getString("ChatRoomOwner"))){
				            	System.out.println("群管理员名字:"+ob2.getString("NickName"));
				            	qunOwner=getURLEncoderString(ob2.getString("NickName"));
				            	
				            }
			        }
            	}
            	return "{\"groupName\":"+"\""+getURLEncoderString(groupNickName)+"\""+
            	",\"groupCount\":"+ob.getString("MemberCount")+
            	",\"groupOwner\":"+"\""+qunOwner+"\""+"}";
            }
            
            
        }
		} catch (Exception e) {
			return "{\"groupName\":"+"\""+getURLEncoderString(groupNickName)+"\""+
	            	",\"groupCount\":0"+
	                    	",\"groupOwner\":\"\""+"}";
		}
	//	}
	
		return "{\"groupName\":"+"\""+getURLEncoderString(groupNickName)+"\""+
    	",\"groupCount\":0"+
            	",\"groupOwner\":\"\""+"}";
	}
	public String UTF8toGBK(String name){
		if(name != ""&&name != null){
		try {
			
			String str = new String(name.getBytes("UTF-8"),"GBK");
			return str;
		} catch (UnsupportedEncodingException e) {
			return null;
		} 
		}
		return null;
	}
	public static String getURLEncoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

	
}